package com.pwrd.minichat;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther syc.
 * @Date 2016/10/31 15:33.
 */
public class ChatServer {

    public List<Client> clients = new ArrayList<Client>();

    public static void main(String[] args) {
        new ChatServer().start();

    }

    public void start() {
        ServerSocket serverSocket = null;
        Socket socket = null;
        boolean started = false;
        //创建server的socket
        try {
            serverSocket = new ServerSocket(8888);
            started = true;
        } catch (IOException e) {
            e.printStackTrace();

            if (serverSocket == null) {
                System.out.println("端口使用中");
                System.exit(0);
            }
        }

        //每个线程负责处理一个连接socket
        try {
            while (started) {
                socket = serverSocket.accept();
                System.out.println("This server has connected to client");
                Client client = new Client(socket);
                clients.add(client);
                new Thread(client).start();
            }
        } catch (IOException e) {
            e.printStackTrace();

        } finally {
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    class Client implements Runnable {

        private Socket socket;
        private DataInputStream dis;
        private DataOutputStream dos;
        private boolean connected = false;

        public Client(Socket socket) {
            this.socket = socket;
            try {
                dis = new DataInputStream(socket.getInputStream());
                dos = new DataOutputStream(socket.getOutputStream());
                connected = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void send(String s) {
            try {
                dos.writeUTF(s);
            } catch (IOException e) {
                clients.remove(this);
                System.out.println("This client has disconnected that remove from list");
            }
        }

        @Override
        public void run() {
            while (connected) {

                try {
                    String s = dis.readUTF();

                    for (int i = 0; i < clients.size(); i++) {
                        Client client = clients.get(i);
                        client.send(s);
                    }

                    System.out.println("The msg from client is :" + s);

                } catch (EOFException eof) {

                } catch (SocketException e) {
                    disconnect();
                    System.out.println("Client closed");

                } catch (IOException e) {
                    disconnect();
                    e.printStackTrace();
                }
            }
        }

        private void disconnect() {
            try {

                connected = false;

                if (dis != null) {
                    dis.close();
                }

                if (dos != null) {
                    dos.close();
                }

                if (socket != null) {
                    socket.close();
                }

            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}
